/**
 * 寄件
 * 
 * @author 李静波
 */

function initProvince() {
	$.post(BASE_URL + "Home/Express/queryProvince", {}, function(result) {
				var editToProvince = $("#editToProvince");
				var editFromProvince = $("#editFromProvince");
				editToProvince.empty();
				editFromProvince.empty();
				for (var i = 0; i < result.length; i++) {
					var item = result[i];
					var opt = $("<option>").text(item.name).val(item.id);
					editToProvince.append(opt);

					var opt = $("<option>").text(item.name).val(item.id);
					editFromProvince.append(opt);
				}
				editToProvince[0].selectedIndex = -1;
				editFromProvince[0].selectedIndex = -1;
			});
}

function queryToCity() {
	$.post(BASE_URL + "Home/Express/queryCity", {
				id : $("#editToProvince").val()
			}, function(result) {
				$("#editToDistrict").empty();

				var edit = $("#editToCity");
				edit.empty();
				for (var i = 0; i < result.length; i++) {
					var item = result[i];
					var opt = $("<option>").text(item.name).val(item.id);
					edit.append(opt);
				}

				edit[0].selectedIndex = -1;
			});
}

function queryToDistrict() {
	$.post(BASE_URL + "Home/Express/queryDistrict", {
				id : $("#editToCity").val()
			}, function(result) {
				var edit = $("#editToDistrict");
				edit.empty();
				for (var i = 0; i < result.length; i++) {
					var item = result[i];
					var opt = $("<option>").text(item.name).val(item.id);
					edit.append(opt);
				}

				edit[0].selectedIndex = -1;
			});
}

function queryFromCity() {
	$.post(BASE_URL + "Home/Express/queryCity", {
				id : $("#editFromProvince").val()
			}, function(result) {
				$("#editFromDistrict").empty();

				var edit = $("#editFromCity");
				edit.empty();
				for (var i = 0; i < result.length; i++) {
					var item = result[i];
					var opt = $("<option>").text(item.name).val(item.id);
					edit.append(opt);
				}

				edit[0].selectedIndex = -1;
			});
}

function queryFromDistrict() {
	$.post(BASE_URL + "Home/Express/queryDistrict", {
				id : $("#editFromCity").val()
			}, function(result) {
				var edit = $("#editFromDistrict");
				edit.empty();
				for (var i = 0; i < result.length; i++) {
					var item = result[i];
					var opt = $("<option>").text(item.name).val(item.id);
					edit.append(opt);
				}

				edit[0].selectedIndex = -1;
			});
}

function onOk() {
	var recName = $("#editRecName").val();
	if (!recName) {
		alert("没有输入收镖人姓名");
		return;
	}

	var destPlace = $("#editToDistrict").val();
	if (!destPlace) {
		alert("没有选择目的地");
		return;
	}

	var recAddress = $("#editRecAddress").val();
	if (!recAddress) {
		alert("没有输入收镖人地址");
		return;
	}

	var recTel = $("#editRecTel").val();
	if (!recTel) {
		alert("没有输入收镖人电话");
		return;
	}

	var sendName = $("#editSendName").val();
	if (!sendName) {
		alert("没有输入托镖人姓名");
		return;
	}

	var startPlace = $("#editFromDistrict").val();
	if (!startPlace) {
		alert("没有选择始发地");
		return;
	}

	var sendAddress = $("#editSendAddress").val();
	if (!sendAddress) {
		alert("没有输入托镖人地址");
		return;
	}

	var sendTel = $("#editSendTel").val();
	if (!sendTel) {
		alert("没有输入托镖人电话");
		return;
	}

	var cargo = $("#editCargo").val();
	if (!cargo) {
		alert("没有输入镖物");
		return;
	}

	var isFragile = $("#editFragile").prop("checked") ? 1 : 0;
	var claimValue = $("#editClaimValue").val();
	var cargoMoney = $("#editCargoMoney").val();
	var memo = $("#editMemo").val();

	if (confirm("请确认是否寄件?")) {
		$.post(BASE_URL + "Home/Express/applyPOST", {
					recName : recName,
					destPlace : destPlace,
					recAddress : recAddress,
					recTel : recTel,
					sendName : sendName,
					startPlace : startPlace,
					sendAddress : sendAddress,
					sendTel : sendTel,
					cargo : cargo,
					fragile : isFragile,
					claimValue : claimValue,
					cargoMoney : cargoMoney,
					memo : memo
				}, function(result) {
					if (result.success) {
						alert("成功提交寄件信息");

						location.replace(BASE_URL);
					} else {
						alert(result.msg);
					}
				});
	}
}

$(function() {
			$("#editToProvince").change(queryToCity);
			$("#editToCity").change(queryToDistrict);

			$("#editFromProvince").change(queryFromCity);
			$("#editFromCity").change(queryFromDistrict);

			// 绑定寄件按钮单击事件
			$("#btnOK").bind("click", onOk);

			initProvince();
		});
